import Magento from "magento-api-rest";
import Qs from 'qs'
let config = {
	url: "http://47.119.123.183/",
	consumerKey: "umq53e6716b6qxtqrw47nmbpzjo7vawj",
	consumerSecret: "jg9whvyqew10pl1atfvv6eat0lexf48t",
	accessToken: "ndra431yh03f3xoaovs1uhb9ljypouiy",
	tokenSecret: "pz7wspv8xzxq5n31wstl5nqlgkd99z8w",
}
export const http = {
	get(url, data, option) {
		return new Promise((resolve, reject) => {
			if (option !== undefined && option.type !== undefined) {
				config.type = option.type
			}
			if (option !== undefined && option.tokenSecret !== undefined ) {
				config.tokenSecret = option.tokenSecret
			}
			if (option !== undefined && option.Authorization !== undefined ) {
				config.axiosConfig = {
					headers: {}
				}
				config.axiosConfig.headers.Authorization = "Bearer " + option.Authorization
			} else {
				delete config.axiosConfig
			}
			new Magento(config).get(url, data).then(res => {
				if (res.status === 200 ) return resolve({error: false, res: res.data});
				return resolve({error: true, res: res});
			}).catch((error) => {
				resolve({error: true, res: error});
			});
		})
	},
	post(url, data, option) {
		return new Promise((resolve, reject) => {
			if (option !== undefined && option.type !== undefined) {
				config.type = option.type
			}
			if (option !== undefined && option.tokenSecret !== undefined ) {
				config.tokenSecret = option.tokenSecret
			}
			if (option !== undefined && option.Authorization !== undefined ) {
				config.axiosConfig = {
					headers: {
						'Content-Type': 'application/json', 
					}
				}
				config.axiosConfig.headers.Authorization = "Bearer " + option.Authorization
			}
			new Magento(config).post(url, data).then(res => {
				if (res.status === 200 ) return resolve({error: false, res: res.data});
				return resolve({error: true, res: res});
			}).catch((error) => {
				resolve({error: true, res: error});
			});
		})
	},
	put(url, data, option) {
		return new Promise((resolve, reject) => {
			if (option !== undefined && option.type !== undefined) {
				config.type = option.type
			}
			if (option !== undefined && option.tokenSecret !== undefined ) {
				config.tokenSecret = option.tokenSecret
			}
			if (option !== undefined && option.Authorization !== undefined ) {
				config.axiosConfig.headers.Authorization = option.Authorization
			}
			new Magento(config).put(url, data).then(res => {
				if (res.status === 200 ) return resolve({error: false, res: res.data});
				return resolve({error: true, res: res});
			}).catch((error) => {
				resolve({error: true, res: error});
			});
		})
	},
	del(url, data, option) {
		return new Promise((resolve, reject) => {
			if (option !== undefined && option.type !== undefined) {
				config.type = option.type
			}
			if (option !== undefined && option.tokenSecret !== undefined ) {
				config.tokenSecret = option.tokenSecret
			}
			if (option !== undefined && option.Authorization !== undefined ) {
				config.axiosConfig = {
					headers: {
						'Content-Type': 'application/json', 
					}
				}
				config.axiosConfig.headers.Authorization = "Bearer " + option.Authorization
			}
			new Magento(config).delete(url, data).then(res => {
				if (res.status === 200 ) return resolve({error: false, res: res.data});
				return resolve({error: true, res: res});
			}).catch((error) => {
				resolve({error: true, res: error});
			});
		})
	}
}

export default http;